package com.kry.mbs.controller;

import java.sql.SQLException;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.kry.mbs.model.Blog;
import com.kry.mbs.model.Manager;
import com.mysql.jdbc.StringUtils;

/**
 * @author jack.song
 * @time 2014年12月26日
 */
public class LoginController extends Controller {

	public void index(){
		System.out.println("loging...........");
		  renderText("你好，JFinal"); 
	}
	
	/*
	 * 登录后台
	 */
	public void logon(){
		System.out.println("logon...........");
		String email=getPara("email");
		String pwd=getPara("pwd");
		System.out.println(String.format("email=%s,password=%s", email,pwd));
		//renderText(String.format("%s,你好，欢迎光临！",userName));
			
			if (checkUser(email,pwd)) {
				setAttr("code", 1);
				//render("amazeui/admin-form.html");
			}else{
				setAttr("code", 0);
				setAttr("msg", "用户名或者密码错误,请重新登录");
				//renderText("登录校验失败，请重新登录......"); 
			}
			renderJson();
	}
	
	private boolean checkUser(String email, String pwd) {
		boolean isOk=false;
		List<Manager> managers=Manager.dao.find("select id,userName,nickName from manager where email='"+email+"' and pwd='"+pwd+"'");
	    if (managers!=null&&managers.size()>0) {
	    	String nickname=managers.get(0).getStr("nickName");
	    	if(!StringUtils.isEmptyOrWhitespaceOnly(nickname)){//nickname!=null&&!("").equals(nickname)
				setSessionAttr("uname", nickname);
			}else{
				setSessionAttr("uname", managers.get(0).getStr("userName"));
			}
	    	System.out.println(1111);
	    	isOk=true;
		}
	    System.out.println(2222);
	    return isOk;
	}

	/*
	 * 登出后台系统
	 */
	public void logout(){
		System.out.println("logout...........");
		setAttr("code", 1);
		renderJson();
	//	render("/login.html");
	}
	
	
	
	public void add() {
	}
	
	public void save() {
		getModel(Blog.class).save();
		redirect("/blog");
		return;
	}
	
	public void delete(){
		Blog.me.deleteById(getParaToInt(0));
		redirect("/blog");
		return;
	}
	
	public void edit(){
		setAttr("blog", Blog.me.findById(getParaToInt(0)));
		render("edit.html");
		return;
	}
	
	public void update(){
		getModel(Blog.class).update();
		setAttr("blog", Blog.me.findById(getPara("blog.id")));
		render("edit.html");
		return;
	}
	
	//测试事务
	public void testTx(){
		try {
			boolean flag = Db.tx(new IAtom() {
				public boolean run() throws SQLException {
					boolean b = getModel(Blog.class).findById(2).set("content", "test1"+System.currentTimeMillis()).update();
					int a = 1/0;
					return b;
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		render("/index.html");
		return;
	}
	
	//测试事务
	@Before(Tx.class)
	public void testTxByAnnoation(){
		try {
			boolean b = getModel(Blog.class).findById(2).set("content", System.currentTimeMillis()).update();
			int a = 1/0;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		render("/index.html");
		return;
	}
}
